<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>栏目页列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索筛选 -->
        <el-form :inline="true" class="user-search">
            <el-form-item>
                <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
            </el-form-item>
        </el-form>
        <!--列表-->
        <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
            element-loading-text="拼命加载中" style="width: 100%;">
            <el-table-column align="center" type="index" label="序号" width="60">
            </el-table-column>

            <el-table-column prop="name" label="专题页名称" width="300">
            </el-table-column>
            
            <el-table-column prop="img" label="专题页图片(移动端展示)" width="300">
                <template slot-scope="scope">
                    <div v-if="scope.row.typeId === 4">{{ scope.row.img }}</div>
                    <img :src="scope.row.img" v-else style="height: 68px;width: 68px;border-radius: 6px" />
                </template>
            </el-table-column>

            <el-table-column prop="typeId" label="专题页类型" width="300">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary">{{ typeName(scope.row.typeId) }}</el-button>
                </template>
            </el-table-column>

            <el-table-column prop="type" label="上传位置" width="300">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary">{{ typeName1(scope.row.type) }}</el-button>
                </template>
            </el-table-column>
            
            <el-table-column align="center" label="操作" min-width="300" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" v-if="scope.row.typeId === 1 || scope.row.typeId === 3" type="success"
                        @click="addDetail(scope.row)">编辑专题页内容</el-button>
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="deleteCategory(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 编辑界面 -->
        <el-dialog :title="title" :visible.sync="editFormVisible" width="60%" @click='closeDialog("edit")'>
            <el-form label-width="170px" ref="editForm">

                <el-form-item label="专题页名称" prop="name">
                    <el-input size="small" v-model="param.name" auto-complete="off" placeholder="请输入专题页名称"></el-input>
                </el-form-item>
                <el-form-item label="专题页类别">
                    <el-select :multiple="false" v-model="param.typeId" placeholder="请选择专题页类别">
                        <el-option v-for="item in typeLst" :key="item.name" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="专题页图片(移动端展示)" v-if="param.type==2">
                    <div class="upload-demo" style="display: flex;align-items: center;">
                        <el-upload action="" :http-request="mainImgUpload" :show-file-list="false"
                            list-type="picture-card">
                            <i class="el-icon-plus"></i>
                        </el-upload>

                        <div style="position:relative;margin-left: 24px;" v-if="param.img">
                            <img :src="param.img" class="avatar" />
                            <div class="deleUrl">
                                <el-button type="danger" icon="el-icon-delete" circle @click="delImageClick()">
                                </el-button>
                            </div>
                        </div>
                    </div>
                </el-form-item>
                
                <el-form-item label="移动端跳转链接" v-if="param.type==2">
                    <el-input size="small" v-model="param.url" placeholder="请输入链接"></el-input>
                </el-form-item>
                <el-form-item label="pc跳转链接" v-if="param.type==1">
                    <el-input size="small" v-model="param.img" placeholder="请输入链接"></el-input>
                </el-form-item>
                <el-form-item label="上传位置">
                    <el-select :multiple="false" v-model="param.type" placeholder="请选择上传位置">
                        <el-option v-for="item in typeLst1" :key="item.name" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序（值越小越靠前）">
                    <el-input size="small" v-model="param.sort" placeholder="请输入排序值"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
                <el-button size="small" type="primary" :loading="loading" class="title" @click="addCategory()"
                    v-if="title == '添加'">添加</el-button>
                <el-button size="small" type="primary" :loading="loading" class="title" @click="upCategory()"
                    v-if="title == '修改'">修改</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {
    addIndex, selectIndexs, updateIndex, deleteIndexByIds
} from "../../api/userMG";
import client from "@/utils/ossClient.js";

import Pagination from "../../components/Pagination";
export default {
    data() {
        return {
            loading: false, //是显示加载
            editFormVisible: false, //控制编辑页面显示与隐藏
            menuAccessshow: false, //控制数据权限显示与隐藏
            title: "添加",
            name: "",
            sort_order: "",
            // 删除
            seletedata: {
                ids: "",
                token: localStorage.getItem("logintoken"),
            },
            typeLst: [
                {
                    id: 1,
                    name: '雅思托福(类型)',
                },
                {
                    id: 2,
                    name: '国际学校类型',
                },
                {
                    id: 3,
                    name: '专题页类型',
                },
                {
                    id: 4,
                    name: '链接类型'
                }
            ],
            param: {
                img: '',//专题页封面
                name: '',//专题页名称
                type: '',
                typeId: '',//专题页类型id
                sort: '',//排序
                url:'',
            },
            listData: [], //用户数据
            // 选中
            checkmenu: [],
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 10,
                total: 10,
            },
            typeLst1: [
                {
                    name: 'pc端',
                    id: 1,
                },
                {
                    name: '移动端',
                    id: 2,
                }
            ]
        };
    },
    // 注册组件
    components: {
        Pagination,
    },

    computed: {
        typeName() {
            return typeId => {
                let name = ''
                this.typeLst.forEach(item => {
                    if (item.id == typeId) {
                        name = item.name

                    }
                })
                return name
            }
        },
        typeName1() {
            return type => {
                let name = ''
                this.typeLst1.forEach(item => {
                    if (item.id == Number(type)) {
                        name = item.name

                    }
                })
                return name
            }
        }
    },
    /**
     * 数据发生改变
     */

    watch: {},

    /**
     * 创建完毕
     */

    created() {
        this.getdata()
    },

    /**
     * 里面的方法只有被调用才会执行
     */

    methods: {
        delImageClick() {
            this.param.img = ''
        },
        addDetail(item) {
            localStorage.setItem('columnPageItem', JSON.stringify(item))
            if (item.typeId == 1) {
                //雅思托福类型
                this.$router.push({ path: '/columnPage/columnPageDetail' });
            } else {
                //富文本专题页设置rich text
                this.$router.push({ path: '/columnPage/columnPageRichText' });
            }
        },
        mainImgUpload(file) {
            //判断扩展名
            const tmpcnt = file.file.name.lastIndexOf(".");
            const exname = file.file.name.substring(tmpcnt + 1);
            const names = ["jpg", "jpeg", "webp", "png", "bmp"];
            if (names.indexOf(exname) < 0) {
                this.$message.error("不支持的格式!");
                return;
            }
            const fileName = file.file.uid + "." + exname;
            client
                .put(fileName, file.file)
                .then((res) => {
                    if (res.url) {
                        this.param.img = res.url;
                    } else {
                        this.$message.error("文件上传失败");
                    }
                })
                .catch((err) => { });
        },

        // 添加分类
        addCategory() {
            this.loading = true;
            addIndex({
                ...this.param
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.editFormVisible = false
                    this.$message({
                        type: "success",
                        message: res.msg,
                    });
                    this.getdata();
                } else {
                    this.editFormVisible = false
                    this.$message.error(res.msg);
                }
            });
        },
        // 修改
        upCategory() {
            // this.loading = true;
            updateIndex({
                ...this.param
            }).then((res) => {
                // this.loading = false;
                if (res.code == 200) {
                    this.editFormVisible = false
                    this.$message({
                        type: "success",
                        message: res.msg,
                    });
                    this.getdata();
                } else {
                    this.$message.error(res.msg);
                }
            });
        },

        // 获取数据方法
        getdata() {
            this.loading = true;
            selectIndexs().then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.listData = res.data.data;

                    return;
                }
                this.$message({
                    type: "info",
                    message: res.msg,
                });
            });
        },
        // 分页插件事件
        callFather(parm) {
            this.formInline.page = parm.currentPage;
            this.formInline.limit = parm.pageSize;
            this.getdata(parm.currentPage);
        },

        //显示编辑界面
        handleEdit: function (row) {

            this.editFormVisible = true;
            if (row != undefined && row != "undefined") {
                this.title = "修改";
                this.param.id = row.id;
                this.param.name = row.name;
                this.param.type = Number(row.type);
                this.param.typeId = row.typeId;
                this.param.img = row.img;
                this.param.sort = row.sort;
                this.param.url=row.url

            } else {
                
                this.title = "添加";
                delete this.param.id
                this.param.name = "";
                this.param.img = "";
                this.param.type = "";
                this.param.typeId = "";
                this.param.sort = "";
                this.param.url=''
            }
        },

        // 删除
        deleteCategory(index, row) {
            this.$confirm("确定要删除吗?", "信息", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    deleteIndexByIds({ id: row.id })
                        .then((res) => {
                            if (res.code == 200) {
                                this.$message({
                                    type: "success",
                                    message: "已删除！",
                                });
                                this.getdata();
                            } else {
                                this.$message({
                                    type: "info",
                                    message: res.msg,
                                });
                            }
                        })
                        .catch((err) => {
                            this.loading = false;
                            this.$message.error("删除失败，请稍后再试！");
                        });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },

        // 关闭编辑、增加弹出框
        closeDialog(dialog) {
            if (dialog == "edit") {
                this.editFormVisible = false;
            } else if (dialog == "perm") {
                this.menuAccessshow = false;
            }
        },
    },
};
</script>

<style scoped>
.user-search {
    margin-top: 20px;
}

.userRole {
    width: 100%;
}
</style>